package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.ConstantItem;
import his.entity.Department;
import his.entity.RegistLevel;
import his.entity.User;
import his.persistence.ConstantItemMapper;
import his.persistence.DepartmentMapper;
import his.persistence.RegistLevelMapper;
import his.persistence.UserMapper;
import his.service.DepartmentService;
import his.service.UserService;
import his.vo.DoctorVo;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserMapperImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Autowired
    ConstantItemMapper constantItemMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    RegistLevelMapper registLevelMapper;

    @Override
    public CommonResponse<List<DoctorVo>> getall_doctors_byword(String keywords) {

        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("RealName",keywords);
        List<User> userList1=userMapper.selectList(queryWrapper1);
        List<DoctorVo> doctorVoList = new ArrayList<>();
        if(userList1.size()>0){
            for(int i=0;i<userList1.size();i++){
                DoctorVo doctorVo=new DoctorVo();
                doctorVo.setRealName(userList1.get(i).getRealName());
                int deptid=userList1.get(i).getDeptID();
                QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
                queryWrapper2.eq("ID",deptid);
                Department department=departmentMapper.selectOne(queryWrapper2);
                doctorVo.setDeptname(department.getDeptName());
                int doctitleid=userList1.get(i).getDocTitleID();
                QueryWrapper<ConstantItem> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("ID",doctitleid);
                ConstantItem constantItem=constantItemMapper.selectOne(queryWrapper3);
                doctorVo.setDoctitle(constantItem.getConstantName());
                int registid=userList1.get(i).getRegistLeID();
                QueryWrapper<RegistLevel> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("ID",registid);
                RegistLevel registLevel=registLevelMapper.selectOne(queryWrapper4);
                doctorVo.setRegistfee(registLevel.getRegistFee());
                doctorVo.setRegistname(registLevel.getRegistName());
                doctorVoList.add(doctorVo);
            }
            return CommonResponse.createForSuccess(doctorVoList);
        }else{
            QueryWrapper<ConstantItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ConstantName",keywords);
            List<ConstantItem> constantItemList=constantItemMapper.selectList(queryWrapper2);
            if(constantItemList.size()>0){
                int constantid=constantItemList.get(0).getID();
                QueryWrapper<User> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("DocTitleID",constantid);
                List<User> userList2=userMapper.selectList(queryWrapper3);
                for(int i=0;i<userList2.size();i++){
                    DoctorVo doctorVo=new DoctorVo();
                    doctorVo.setRealName(userList2.get(i).getRealName());
                    int deptid=userList2.get(i).getDeptID();
                    QueryWrapper<Department> queryWrapper9=new QueryWrapper<>();
                    queryWrapper9.eq("ID",deptid);
                    Department department=departmentMapper.selectOne(queryWrapper9);
                    doctorVo.setDeptname(department.getDeptName());
                    int doctitleid=userList2.get(i).getDocTitleID();
                    QueryWrapper<ConstantItem> queryWrapper10=new QueryWrapper<>();
                    queryWrapper10.eq("ID",doctitleid);
                    ConstantItem constantItem=constantItemMapper.selectOne(queryWrapper10);
                    doctorVo.setDoctitle(constantItem.getConstantName());
                    int registid=userList2.get(i).getRegistLeID();
                    QueryWrapper<RegistLevel> queryWrapper11=new QueryWrapper<>();
                    queryWrapper11.eq("ID",registid);
                    RegistLevel registLevel=registLevelMapper.selectOne(queryWrapper11);
                    doctorVo.setRegistfee(registLevel.getRegistFee());
                    doctorVo.setRegistname(registLevel.getRegistName());
                    doctorVoList.add(doctorVo);
                }
                return CommonResponse.createForSuccess(doctorVoList);

            }else{
                QueryWrapper<Department> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("DeptName",keywords);
                List<Department> departmentList=departmentMapper.selectList(queryWrapper4);
                if(departmentList.size()>0){
                    int departmentid=departmentList.get(0).getID();
                    QueryWrapper<User> queryWrapper5=new QueryWrapper<>();
                    queryWrapper5.eq("DeptID",departmentid);
                    List<User> userList3=userMapper.selectList(queryWrapper5);
                    for(int i=0;i<userList3.size();i++){
                        DoctorVo doctorVo=new DoctorVo();
                        doctorVo.setRealName(userList3.get(i).getRealName());
                        int deptid=userList3.get(i).getDeptID();
                        QueryWrapper<Department> queryWrapper8=new QueryWrapper<>();
                        queryWrapper8.eq("ID",deptid);
                        Department department=departmentMapper.selectOne(queryWrapper8);
                        doctorVo.setDeptname(department.getDeptName());
                        int doctitleid=userList3.get(i).getDocTitleID();
                        QueryWrapper<ConstantItem> queryWrapper6=new QueryWrapper<>();
                        queryWrapper6.eq("ID",doctitleid);
                        ConstantItem constantItem=constantItemMapper.selectOne(queryWrapper6);
                        doctorVo.setDoctitle(constantItem.getConstantName());
                        int registid=userList3.get(i).getRegistLeID();
                        QueryWrapper<RegistLevel> queryWrapper7=new QueryWrapper<>();
                        queryWrapper7.eq("ID",registid);
                        RegistLevel registLevel=registLevelMapper.selectOne(queryWrapper7);
                        doctorVo.setRegistfee(registLevel.getRegistFee());
                        doctorVo.setRegistname(registLevel.getRegistName());
                        doctorVoList.add(doctorVo);
                    }
                    return CommonResponse.createForSuccess(doctorVoList);

                }else{
                    QueryWrapper<RegistLevel> queryWrapper6=new QueryWrapper<>();
                    queryWrapper6.eq("RegistName",keywords);
                    List<RegistLevel> registLevelList=registLevelMapper.selectList(queryWrapper6);
                    if(registLevelList.size()>0){
                        int registid=registLevelList.get(0).getID();
                        QueryWrapper<User> queryWrapper7=new QueryWrapper<>();
                        queryWrapper7.eq("RegistLeID",registid);
                        List<User> userList4=userMapper.selectList(queryWrapper7);
                        for(int i=0;i<userList4.size();i++){
                            DoctorVo doctorVo=new DoctorVo();
                            doctorVo.setRealName(userList4.get(i).getRealName());
                            int deptid=userList4.get(i).getDeptID();
                            QueryWrapper<Department> queryWrapper12=new QueryWrapper<>();
                            queryWrapper12.eq("ID",deptid);
                            Department department=departmentMapper.selectOne(queryWrapper12);
                            doctorVo.setDeptname(department.getDeptName());
                            int doctitleid=userList4.get(i).getDocTitleID();
                            QueryWrapper<ConstantItem> queryWrapper13=new QueryWrapper<>();
                            queryWrapper13.eq("ID",doctitleid);
                            ConstantItem constantItem=constantItemMapper.selectOne(queryWrapper13);
                            doctorVo.setDoctitle(constantItem.getConstantName());
                            int registid1=userList4.get(i).getRegistLeID();
                            QueryWrapper<RegistLevel> queryWrapper14=new QueryWrapper<>();
                            queryWrapper14.eq("ID",registid1);
                            RegistLevel registLevel=registLevelMapper.selectOne(queryWrapper14);
                            doctorVo.setRegistfee(registLevel.getRegistFee());
                            doctorVo.setRegistname(registLevel.getRegistName());
                            doctorVoList.add(doctorVo);
                        }
                        return CommonResponse.createForSuccess(doctorVoList);

                    }else{
                        return CommonResponse.createForError("未找到该医生，请输入正确关键词");
                    }
                }
            }
        }

    }

    @Override
    public CommonResponse<List<DoctorVo>> getall_doctors() {
        List<DoctorVo> doctorVoList = new ArrayList<>();
        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("UseType",3);
        List<User> userList=userMapper.selectList(queryWrapper1);
        if(userList.size()>0){
            for(int i=0;i<userList.size();i++){
                DoctorVo doctorVo=new DoctorVo();
                doctorVo.setRealName(userList.get(i).getRealName());
                int deptid=userList.get(i).getDeptID();
                QueryWrapper<Department> queryWrapper2=new QueryWrapper<>();
                queryWrapper2.eq("ID",deptid);
                Department department=departmentMapper.selectOne(queryWrapper2);
                doctorVo.setDeptname(department.getDeptName());
                int doctitleid=userList.get(i).getDocTitleID();
                QueryWrapper<ConstantItem> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("ID",doctitleid);
                ConstantItem constantItem=constantItemMapper.selectOne(queryWrapper3);
                doctorVo.setDoctitle(constantItem.getConstantName());
                int registid=userList.get(i).getRegistLeID();
                QueryWrapper<RegistLevel> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("ID",registid);
                RegistLevel registLevel=registLevelMapper.selectOne(queryWrapper4);
                doctorVo.setRegistfee(registLevel.getRegistFee());
                doctorVo.setRegistname(registLevel.getRegistName());
                doctorVoList.add(doctorVo);
            }
            return CommonResponse.createForSuccess(doctorVoList);

        }else{
            return CommonResponse.createForError("搜索失败");
        }

    }

    @Override
    public CommonResponse<List<User>> getall_users_byname(String keywords) {

        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("UserName",keywords);
        List<User> userList1=userMapper.selectList(queryWrapper1);
        if(userList1.size()>0){
            return CommonResponse.createForSuccess(userList1);
        }else{
            QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("RealName",keywords);
            List<User> userList2=userMapper.selectList(queryWrapper2);
            if(userList2.size()>0){
                return CommonResponse.createForSuccess(userList2);
            }else{
                return CommonResponse.createForSuccessMessage("未找到对应的用户");
            }
        }

    }

    @Override
    public CommonResponse<List<User>> getall_users() {

        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        List<User> userList=userMapper.selectList(queryWrapper);
        if(userList.size()>0){
            return CommonResponse.createForSuccess(userList);
        }else{
            return CommonResponse.createForError("查找失败");
        }

    }

    @Override
    public CommonResponse<List<User>> delete_users(String real_name) {
        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("RealName",real_name);
        List<User> userList=userMapper.selectList(queryWrapper1);
        if(userList.size()>0){
            QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("RealName",real_name);
            int result=userMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(userList);
        }else{
            return CommonResponse.createForError("删除失败");
        }

    }

    @Override
    public CommonResponse<List<User>> update_users(String name, String userName, String password, String RealName, int useType, int docTitleID, String isScheduling, int deptID,int registLeID) {
        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("UserName",name);
        List<User> userList1=userMapper.selectList(queryWrapper1);
        if(userList1.size()>0){
            int id=userList1.get(0).getID();
            QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("UserName",name);
            User user=new User();
            user.setUserName(userName);
            user.setPassword(password);
            user.setRealName(RealName);
            user.setUseType(useType);
            user.setDocTitleID(docTitleID);
            user.setIsScheduling(isScheduling);
            user.setDeptID(deptID);
            user.setRegistLeID(registLeID);
            int result=userMapper.update(user,queryWrapper2);
            QueryWrapper<User> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("ID",id);
            List<User> userList3=userMapper.selectList(queryWrapper3);
            return CommonResponse.createForSuccess(userList3);
        }else{
            QueryWrapper<User> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("RealName",name);
            List<User> userList2=userMapper.selectList(queryWrapper3);
            if(userList2.size()>0){
                int id=userList2.get(0).getID();
                QueryWrapper<User> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("RealName",name);
                User user=new User();
                user.setUserName(userName);
                user.setPassword(password);
                user.setRealName(RealName);
                user.setUseType(useType);
                user.setDocTitleID(docTitleID);
                user.setIsScheduling(isScheduling);
                user.setDeptID(deptID);
                user.setRegistLeID(registLeID);
                int result=userMapper.update(user,queryWrapper4);
                QueryWrapper<User> queryWrapper5=new QueryWrapper<>();
                queryWrapper5.eq("ID",id);
                List<User> userList3=userMapper.selectList(queryWrapper5);
                return CommonResponse.createForSuccess(userList3);
            }else{
                return CommonResponse.createForError("修改失败");
            }
        }

    }


    @Override
    public CommonResponse<List<User>> add_user(String userName, String password, String RealName, int useType, int docTitleID, String isScheduling, int deptID,int registLeID) {

        QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("UserName",userName);
        List<User> userList1=userMapper.selectList(queryWrapper1);
        if(userList1.size()>0){
            return CommonResponse.createForError("新增失败，用户名已存在");
        }else{
            User user=new User();
            user.setUserName(userName);
            user.setPassword(password);
            user.setRealName(RealName);
            user.setUseType(useType);
            user.setDocTitleID(docTitleID);
            user.setIsScheduling(isScheduling);
            user.setDeptID(deptID);
            user.setDelMark(1);
            user.setRegistLeID(registLeID);
            userMapper.insert(user);
            QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("UserName",userName);
            List<User> userList2=userMapper.selectList(queryWrapper2);
            return CommonResponse.createForSuccess(userList2);
        }

    }
}
